﻿var path = this.path;
define([
    'dojo/_base/declare',
    'esri/geometry/Point',
    'geomap/plugins/layers/DivGraphicsLayer',
    'geomap/plugins/DivGraphic',
], function (declare, Point, BaseCustomGraphicDivLayer, DivGraphic) {
    var CustomGraphicDivLayer = declare([BaseCustomGraphicDivLayer], {
        setData: function (mapView, layer) {
            var me = this;
            var mapUtils = mapView.mapUtils;
            me.imageUrl = path + './../../one-map/1.0.0/src/mode/arcgis/css/images/square.svg';
            me._defaultWidth = 12;
            me.__defaultHeight = 12;
            var options = layer.layer || {};
            me.options = options;
            me.symbolOptions = Object.assign({ offsetx: 0, offsety: 0 }, options.options);
            var data = $.extend(true, [], options.data);
            if (!data || data.length < 1) {
                if (typeof options.callback == 'function') {
                    options.callback(layer);
                }
                return;
            }
            var lngLatArray = mapUtils.toLngLatArray(mapView, data, options);
            var symbolFieldName = options.symbolFieldName || mapView.getSymbolFieldName();

            var map = mapView.getMap();
            me.setScale(map, me._element);

            me.convertSr(mapView, lngLatArray, callback);

            function callback(lngLatArray, sr) {
                lngLatArray.map(function (lngLat, index) {
                    var point = mapUtils.createPoint(mapView, lngLat, { sr: sr });
                    var symbol = lngLat[symbolFieldName] || options.symbol || {};
                    var div = new DivGraphic(point);
                    div.__graphic = lngLat.__graphic || {};
                    div.__graphic.__divGraphic = div;

                    var eleJQ = $(div.getElement());
                    eleJQ
                        .addClass(symbol.class)
                        .css(symbol.css || {})
                        .html(symbol.html);
                    me.add(div);

                    div.attributes = lngLat;

                    me.setOffset(me.symbolOptions, eleJQ);

                    me.setCollision(div, options);

                    if (index == lngLatArray.length - 1) {
                        if (typeof options.callback == 'function') {
                            options.callback(layer);
                        }
                    }
                });
            }
        },
    });
    return CustomGraphicDivLayer;
});
